from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import torch_npu
from torch_npu.contrib import transfer_to_npu
if not torch.npu.is_available():
    raise RuntimeError("NPU is not available. Please check your environment.")

# 指定设备
device = torch.device("npu")
#device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained("此处更改为模型参数路径", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    "此处更改为模型参数路径",
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    trust_remote_code=True
    ).to(device).eval()
inputs = tokenizer.apply_chat_template([{"role": "user", "content": "write a quick sort"}], add_generation_prompt=True, tokenize=True, return_tensors="pt", return_dict=True).to(device)
with torch.no_grad():
    outputs = model.generate(**inputs,max_length=1024)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0], skip_special_tokens=True))